package com.cennavi.minemap.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cennavi.minemap.bean.DataBean;
import com.cennavi.minemap.bean.MessageBean;
import com.cennavi.minemap.bean.TokenBean;
import com.cennavi.minemap.bean.TokenSolusIdBean;
import com.cennavi.minemap.bean.TokenWithSoluBean;
import com.cennavi.minemap.service.TokenService;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@ComponentScan(basePackages = { "com.cennavi.minemap.service" })
@RequestMapping("/service/token")
public class TokenController {

	@Value("${spring.minemap.token.count}")
	private int tokenCount;

	ObjectMapper objectMapper = new ObjectMapper();

	@Autowired(required = false)
	private TokenService tokenService;

	@RequestMapping(value = "/limit")
	public @ResponseBody Integer getLimit() {
		return tokenCount;
	}

	@RequestMapping(value = "/apply", method = RequestMethod.POST)
	public @ResponseBody MessageBean apply(@RequestBody String json) throws Exception {
		TokenBean tokenBean = objectMapper.readValue(json, TokenBean.class);
		MessageBean messageBean = new MessageBean();
		List<TokenBean> list = tokenService.apply(tokenBean);
		if (list == null) {
			messageBean.setErrcode(-2);
			messageBean.setErrmsg(String.format("无可用token申请，每个用户最多可以申请%d个token", tokenCount));
		} else if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("token apply error");
		} else {
			DataBean<TokenBean> dataBean = new DataBean<TokenBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody MessageBean update(@RequestBody String json) throws Exception {
		TokenBean tokenBean = objectMapper.readValue(json, TokenBean.class);
		MessageBean messageBean = new MessageBean();
		int status = tokenService.update(tokenBean);
		if (status < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("token update error");
		} else {
			messageBean.setErrcode(0);
			messageBean.setErrmsg(null);
		}
		return messageBean;
	}

	// curl http://localhost:8080/service/token/list/1
	@RequestMapping("/list/{userId}")
	public @ResponseBody MessageBean queryByUserId(@PathVariable int userId) {
		MessageBean messageBean = new MessageBean();
		List<TokenBean> list = tokenService.queryByUserId(userId);
		if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("token query error");
		} else {
			DataBean<TokenBean> dataBean = new DataBean<TokenBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping("/listOfNoSolu/{userId}")
	public @ResponseBody MessageBean queryByUserIdOfNoSolu(@PathVariable int userId) {
		MessageBean messageBean = new MessageBean();
		List<TokenBean> list = tokenService.queryByUserIdOfNoSolu(userId);
		if (list == null) {
			messageBean.setErrcode(-2);
			messageBean.setErrmsg("无可用token");
		} else if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("token query error");
		} else {
			DataBean<TokenBean> dataBean = new DataBean<TokenBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	// curl http://localhost:8080/service/token/1
	@RequestMapping("/{id}")
	public @ResponseBody MessageBean detail(@PathVariable String id) {
		MessageBean messageBean = new MessageBean();
		List<TokenBean> list = tokenService.queryById(id);
		if (list.size() == -1) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("token query error");
		} else {
			DataBean<TokenBean> dataBean = new DataBean<TokenBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/solu", method = RequestMethod.POST)
	public @ResponseBody MessageBean insertTokenSolu(@RequestBody String json) throws Exception {
		TokenSolusIdBean tokenSolusBean = objectMapper.readValue(json, TokenSolusIdBean.class);
		MessageBean messageBean = new MessageBean();
		int status = tokenService.insertTokenSolu(tokenSolusBean);
		if (status < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("token solu relation error");
		} else {
			messageBean.setErrcode(0);
			messageBean.setErrmsg(null);
		}
		return messageBean;
	}

	@RequestMapping("/list/solu/{option}")
	public @ResponseBody MessageBean queryBySoluUserId(@PathVariable String option, @RequestParam("soluId") int soluId,
			@RequestParam("userId") int userId) {
		MessageBean messageBean = new MessageBean();
		List<TokenWithSoluBean> list = null;

		list = tokenService.queryBySoluUserId(option, soluId, userId);

		if (list == null) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("token query error");
		} else {
			DataBean<TokenWithSoluBean> dataBean = new DataBean<TokenWithSoluBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

}
